/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.cax.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.cax.domain.CaxFileInfo;
import com.comac.ins.cax.domain.bo.CaxFileInfoBo;
import com.comac.ins.cax.domain.vo.CaxFileInfoVo;
import com.comac.ins.cax.enums.CaxConstants;
import com.comac.ins.cax.service.ICaxFileInfoService;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 源文件信息管理
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cax/sourceFile")
public class CaxFileInfoController extends BaseController {
    @Autowired
    private ICaxFileInfoService iCaxFileInfoService;

    /**
     * 文件上传
     * @param file
     * @param type
     * @return
     */
    @Log(title = CaxConstants.MODULE_SOURCE_FILE, businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping("/upload")
    @SaCheckPermission("cax:sourceFile:upload")
    public R<List<CaxFileInfo>> upload(@RequestParam("files") MultipartFile[] file,
                                        @RequestParam(value = "type") String type) {
        return R.ok(iCaxFileInfoService.batchUploadFile(file, type));
    }

    /**
     * 文件下载
     * @param response
     * @param id
     * @return
     */
    @GetMapping("/download/{id}")
    @SaCheckPermission("cax:sourceFile:download")
    public void download(HttpServletResponse response, @NotNull(message = "ID不能为空") @PathVariable("id") Long id) {
        iCaxFileInfoService.download(response, id);
    }

    /**
     * 源文件信息导出
     * @param response
     * @return
     */
    @Log(title = CaxConstants.MODULE_SOURCE_FILE, businessType = BusinessType.EXPORT)
    @PostMapping("/exportData")
    @SaCheckPermission("cax:sourceFile:exportData")
    public void exportData(HttpServletResponse response,CaxFileInfoBo bo) {
        iCaxFileInfoService.export(response,bo);
    }

    /**
     * 源文件信息分页查询
     * @param bo
     * @return
     */
    @PostMapping("/list")
    @SaCheckPermission("cax:sourceFile:list")
    public TableDataInfo<CaxFileInfoVo> list(@RequestBody CaxFileInfoBo bo) {
        return iCaxFileInfoService.queryPageList(bo);
    }

    /**
     * 源文件批量删除
     * @param ids
     * @return
     */
    @Log(title = CaxConstants.MODULE_SOURCE_FILE, businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @SaCheckPermission("cax:sourceFile:delete")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iCaxFileInfoService.deleteBatchIds(Arrays.asList(ids)));
    }
}
